<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Kame
 * Date: 18/03/13
 * Time: 10:09 PM
 */

namespace Site;

use Site\Model\PointType;
use Site\Model\PointTypeTable;
use Site\Model\User,
    Site\Model\UserTable,
    Site\Model\Point,
    Site\Model\PointTable,
    Zend\Authentication\AuthenticationService,
    Zend\Db\ResultSet\ResultSet,
    Zend\Db\TableGateway\TableGateway,
    Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

class Module
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Site\Model\UserTable' => function ($sm) {
                    $tableGateway = $sm->get('UserTableGateway');
                    $table = new UserTable($tableGateway);
                    return $table;
                },
                'UserTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new User());
                    return new TableGateway('usuario', $dbAdapter, null, $resultSetPrototype);
                },
                'Site\Model\PointTable' => function ($sm) {
                    $tableGateway = $sm->get('PointTableGateway');
                    $table = new PointTable($tableGateway);
                    return $table;
                },
                'PointTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Point());
                    return new TableGateway('punto', $dbAdapter, null, $resultSetPrototype);
                },
                'AuthService' => function ($sm) {
                    $dbAdapter           = $sm->get('Zend\Db\Adapter\Adapter');
                    $dbTableAuthAdapter  = new DbTableAuthAdapter($dbAdapter,
                        'usuario','nick','pase');

                    $authService = new AuthenticationService();
                    $authService->setAdapter($dbTableAuthAdapter);
                    return $authService;
                },
                'Site\Model\PointTypeTable' => function ($sm) {
                    $tableGateway = $sm->get('PointTypeTableGateway');
                    $table = new PointTypeTable($tableGateway);
                    return $table;
                },
                'PointTypeTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new PointType());
                    return new TableGateway('tipopunto', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
}